#include "windowheader.h"
#include <QMouseEvent>
#include <QPushButton>
#include <QPainter>
#include "../terminalglobals.h"
#include <QApplication>
#include "../executionterminalform.h"

using namespace ExecTerminalImplNS;

const int TOP_BUTTONS_MARGIN = 0; 
const int RIGHT_BUTTONS_MARGIN = 0;
const int BUTTONS_SPACING = 4;
const int HEADER_HEIGHT = 26;
const int CLOSE_BUTTON_WIDTH = 19;
const int BUTTON_WIDTH = 16;
const int BUTTON_HEIGHT = HEADER_HEIGHT;
const int ADDITIONAL_WIDTH = 30;
const QColor HEADER_FRAME_COLOR(22, 50, 81);

const int ICON_TOP_MARGIN = 5;
const int ICON_LEFT_MARGIN = 6;

const int TITLE_TOP_MARGIN = 17;
const int TITLE_LEFT_MARGIN = 27;
const QColor TEXT_DARK_COLOR(49, 72, 97);
const QColor TEXT_LIGHT_COLOR(255, 255, 255);

WindowHeader::WindowHeader(QWidget * parent):QWidget(parent),
theMinimizeButton(0),
theExpandButton(0),
theCloseButton(0)
{
    Q_ASSERT(parent && parent->isWindow());
    Q_ASSERT(qobject_cast<ExecutionTerminalForm*>(parent));
    parent->setWindowFlags(Qt::FramelessWindowHint);
    theMinimizeButton = new QPushButton(this);
    theExpandButton = new QPushButton(this);
    theCloseButton = new QPushButton(this);
    theMinimizeButton->setObjectName("minimizeButton");
    theExpandButton->setObjectName("expandButton");
    theCloseButton->setObjectName("closeButton");
    theMinimizeButton->resize(BUTTON_WIDTH, HEADER_HEIGHT);
    theExpandButton->resize(BUTTON_WIDTH, HEADER_HEIGHT);
    theCloseButton->resize(CLOSE_BUTTON_WIDTH, BUTTON_HEIGHT);
    connect(theMinimizeButton, SIGNAL(clicked()), SLOT(minimizeWindow()));
    connect(theExpandButton, SIGNAL(clicked()), SLOT(processExpandButton()));
    connect(theCloseButton, SIGNAL(clicked()), parent, SLOT(close()));
    const int minWidth = 2 * BUTTON_WIDTH + 2 * BUTTONS_SPACING + CLOSE_BUTTON_WIDTH + RIGHT_BUTTONS_MARGIN + ADDITIONAL_WIDTH;
    setMinimumWidth(minWidth);
    setMinimumHeight(HEADER_HEIGHT);
    setMaximumHeight(HEADER_HEIGHT);
    layoutComponents();
    parentWidget()->installEventFilter(this);
}

void WindowHeader::minimizeWindow()
{
    parentWidget()->showMinimized();
}

void WindowHeader::processExpandButton()
{
    parentWidget()->setUpdatesEnabled(false);
    ExecutionTerminalForm * execTerminal = qobject_cast<ExecutionTerminalForm *>(parentWidget());
    if (execTerminal->isNormalView())
    {
        //change header expand button icons
        execTerminal->setMaximizedView();
        setStyleSheet(
            "QPushButton#expandButton {"
            "     background: url(:/Resources/title_button_nofullscreen.png);"
            " }"

            "QPushButton#expandButton:hover {"
            "     background: url(:/Resources/title_button_nofullscreen_hover.png);"
            " }"

            "QPushButton#expandButton:pressed {"
            "     background: url(:/Resources/title_button_nofullscreen_down.png);"
            " }");
    }
    else
    {
        execTerminal->setNormalView();
        setStyleSheet(QString());
    }
    parentWidget()->setUpdatesEnabled(true);
}

void WindowHeader::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    const bool active = isActiveWindow();
    //draw background
    const QPixmap & curBackgrnd = active ? pixmapForPath(":/Resources/title_center.png") 
        : pixmapForPath(":/Resources/title_center_dis.png");
    painter.drawTiledPixmap(QRect(1, 1, width() - 1, height() - 1), curBackgrnd);
    painter.setPen(HEADER_FRAME_COLOR);
    painter.drawRect(QRect(QPoint(0,0), size() - QSize(1,1)));

    //draw icon
    QPixmap pixmap = parentWidget()->windowIcon().pixmap(QSize(22, 22),
        parentWidget()->isEnabled() ? QIcon::Normal
        : QIcon::Disabled);
    painter.drawPixmap(ICON_LEFT_MARGIN, ICON_TOP_MARGIN, pixmap);

    //draw caption
    QFont fnt = parentWidget()->font();
    fnt.setBold(true);
    painter.setFont(fnt);
    painter.setPen(TEXT_DARK_COLOR);
    QString textToDraw = parentWidget()->windowTitle();
    if (!theAdditionalCaption.isEmpty())
    {
        textToDraw += " - " + theAdditionalCaption;
    }
    painter.drawText(TITLE_LEFT_MARGIN + 1,  TITLE_TOP_MARGIN + 1, textToDraw);
    painter.setPen(TEXT_LIGHT_COLOR);
    painter.drawText(TITLE_LEFT_MARGIN, TITLE_TOP_MARGIN, textToDraw);
}

void WindowHeader::resizeEvent(QResizeEvent *)
{
    layoutComponents();
}

void WindowHeader::mousePressEvent(QMouseEvent * e)
{
    if (e->button() == Qt::LeftButton)
    {
        thePreviousMousePos = e->globalPos();
    }
}

void WindowHeader::mouseMoveEvent(QMouseEvent * e)
{
    QWidget * wnd = window();
    if ((e->buttons() & Qt::LeftButton) != 0
        && parentWidget()->windowState() == Qt::WindowNoState
        && static_cast<ExecutionTerminalForm*>(parentWidget())->isNormalView())
    {
        parentWidget()->move(parentWidget()->pos() + e->globalPos() - thePreviousMousePos);
        thePreviousMousePos = e->globalPos();
    }
}

void WindowHeader::layoutComponents()
{
    setUpdatesEnabled(false);
    theCloseButton->move(width() - CLOSE_BUTTON_WIDTH - RIGHT_BUTTONS_MARGIN, 
        TOP_BUTTONS_MARGIN);
    theExpandButton->move(theCloseButton->x() - BUTTONS_SPACING - BUTTON_WIDTH,
        TOP_BUTTONS_MARGIN);
    theMinimizeButton->move(
        theExpandButton->geometry().translated(- BUTTONS_SPACING - BUTTON_WIDTH, 0).topLeft());
    setUpdatesEnabled(true);
}

void WindowHeader::mouseDoubleClickEvent(QMouseEvent *)
{
    processExpandButton();
}

bool WindowHeader::eventFilter(QObject *o, QEvent *e)
{
    if (o == parentWidget() && e->type() == QEvent::ActivationChange)
    {
        update();
    }
    return false;
}

void WindowHeader::setAdditionalCaption(const QString & str)
{
    if (str != theAdditionalCaption)
    {
        theAdditionalCaption = str;
        update();
    }
}




